//@Time:2022/4/9 10:49
//@Author:aFun

package indi.csms_springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import indi.csms_springboot.domain.SCVO;
import indi.csms_springboot.domain.SSFOVO;
import indi.csms_springboot.domain.SSFSVO;
import indi.csms_springboot.domain.User;
import indi.csms_springboot.mapper.StudentMapper;
import indi.csms_springboot.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public IPage<SSFSVO> studentScore(String _currentPage, String _rows, Map<String, String[]> condition,Integer stuId) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        Page<SSFSVO> page=new Page<>(currentPage,rows);
        QueryWrapper<SSFSVO> wrapper=new QueryWrapper<>();

        wrapper.in("c.couId", studentMapper.findCouIdByStuId(stuId));
        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                wrapper.like(key,value);
            }
        }
        wrapper.groupBy("c.couId");
        IPage<SSFSVO> ssfsvoiPage = studentMapper.studentScore(page, wrapper);

        for (SSFSVO record : ssfsvoiPage.getRecords()) {
            Integer rank = studentMapper.setRank(record.getCouId(), stuId);
            record.setRank(rank);
        }

        return ssfsvoiPage;
    }

    @Override
    public Integer findStuId(User user) {
        return studentMapper.findStuIdByAccount(user.getAccount());
    }

    @Override
    public IPage<SCVO> studentCourse(String _currentPage, String _rows, Map<String, String[]> condition, Integer stuId) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        Page<SCVO> page=new Page<>(currentPage,rows);
        QueryWrapper<SCVO> wrapper=new QueryWrapper<>();
        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                wrapper.like(key,value);
            }
        }
        wrapper.eq("ss.stuId",stuId);
        IPage<SCVO> sc = studentMapper.findSC(page, wrapper);

        return sc;
    }

    @Override
    public boolean deleteCourse(Integer ssId) {
        return studentMapper.deleteCourse(ssId);
    }

    @Override
    public IPage<SCVO> showCourseToSelect(String _currentPage, String _rows, Map<String, String[]> condition, Integer stuId) {
        int currentPage = Integer.parseInt(_currentPage);
        int rows = Integer.parseInt(_rows);
        Page<SCVO> page=new Page<>(currentPage,rows);
        QueryWrapper<SCVO> wrapper=new QueryWrapper<>();

        wrapper.notIn("c.couId", studentMapper.findCouIdByStuId(stuId));
        Set<String> keySet = condition.keySet();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                wrapper.like(key,value);
            }
        }
        wrapper.eq("c.judge",1);
        wrapper.eq("c.isopen",1);

        IPage<SCVO> scToSelect = studentMapper.findSCToSelect(page, wrapper);

        return scToSelect;
    }


    @Override
    public boolean selectCourse(SCVO scvo) {
        return studentMapper.selectCourse(scvo.getStuId(),scvo.getCouId(),scvo.getTeaId());
    }


}
